package flextv.core.logic {

import flash.utils.Proxy;
import flash.utils.flash_proxy;

public dynamic class DynamicImplementation extends Proxy {

	private var mtProperties:Object = new Object();


	public function DynamicImplementation() {
	}

	/**
	 * Add a function to the dynamic implementation.
	 */
	protected function addFunction(sName:String, tFunction:Function):void {
	    if (mtProperties[sName] == null) {
	        mtProperties[sName] = tFunction;
	    } else {
		    throw new Error("Duplicate function: " + sName + " in object: " + this);
		}
	}

	/**
	 * Execute the dynamically added functions.
	 */
	override flash_proxy function callProperty(name: *, ...rest): * {
		
		const tFunction:Function = mtProperties[name];
		if(tFunction == null) {
			throw new Error("Function [" + name + "] is not initialized for object: " + this);
		}

		var tReturn:Object = null;
	    try {
	        tReturn = tFunction.apply(mtProperties, rest);
	    } catch (tError: ArgumentError) {
	        throw new Error("Incorrect number of parameters to function: " + name);
	    }
		
		return tReturn;
	}
}
}
